<?PHP

ini_set('include_path',ini_get('include_path').':/home/mermexi/local/lib/PEAR:'); 

require_once('config.php');

// DB init
require_once('DB.php');
$db = DB::connect(sprintf('mysql://%s:%s@%s/%s', HM_DBUSER, HM_DBPASS, HM_DBHOST, HM_DBNAME));
if (PEAR::isError($db)) { die ($db->getMessage()); }
$db->setFetchMode(DB_FETCHMODE_ASSOC);

// $user init
if (isset($_COOKIE['userid'])) $user = $db->getRow('SELECT * FROM users WHERE user_id="' . $_COOKIE['userid'] . '"');
if (!isset($user)) $user = array('user_id' => '', 'name' => '', 'categories' => serialize(array()));
$user['categories'] = unserialize($user['categories']);

// categories list
$categories = $db->getAll('SELECT cat_id, cat_desc FROM categories'); 

// grab the current user's average score per category for display also
$cat_scores_indexed = array();
if ($user['user_id'] != '') {
 $cat_scores = $db->getAll(sprintf('select puzzle_cat, avg(puzzle_score)*100 as avg from scores, puzzles where user_id="%s" && puzzles.puzzle_id = scores.puzzle_id group by puzzle_cat', $user['user_id']));
 foreach ($cat_scores as $score) {
  $cat_scores_indexed[$score['puzzle_cat']] = $score['avg'];
 }
}

if ($_POST['cmd'] == 'save') {

 $user['name'] = trim($_POST['name']);
 $user['userid'] = md5($user['name']);
 $user['categories'] = $_POST['categories'];

 if ($user['name'] == '') $error_msg = 'A name is required to save any preferences!';

 if ($user['name'] != '') {

  $sql_existing_user = sprintf('SELECT * FROM users WHERE user_id="%s" LIMIT 1', $user['userid']);
  $sql_update_user = sprintf('UPDATE users SET categories="%s" WHERE user_id="%s" LIMIT 1', $db->escapeSimple(serialize($user['categories'])), $user['userid']);
  $sql_insert_user = sprintf('INSERT INTO users (user_id, name, categories) VALUES ( "%s", "%s", "%s")', $user['userid'], $user['name'], $db->escapeSimple(serialize($user['categories'])));

  $existing_user = $db->getRow($sql_existing_user);
  if ($existing_user) $result = $db->query($sql_update_user);
  else $result = $db->query($sql_insert_user);

 }

 if ($result) { $result_msg = 'Preferences saved!'; setcookie('userid', $user['userid']); }
 else $result_msg = 'Preferences NOT saved!';

}

echo '<html>' . "\r\n";
echo '<body>' . "\r\n";
if (isset($error_msg)) echo '<div class="error">' . $error_msg . '</div>' . "\r\n";
if (isset($result_msg)) echo '<div class="result">' . $result_msg . '</div>' . "\r\n";
echo '<form action="' . $_SERVER['REQUEST_URI'] . '" method="POST">' . "\r\n";
echo '<input type="hidden" name="cmd" value="save"/>' . "\r\n";
echo '<label>Name (required):<input type="text" name="name" value="' . $user['name'] . '"/></label>' . "\r\n";
echo '<br/>' . "\r\n";
echo '<label>Select Categories</label>' . "\r\n";
echo sprintf('<select name="categories[]" size="%d" multiple>', min(count($categories), 20)) . "\r\n";
foreach ($categories as $category) {
 echo sprintf('<option value="%d"%s>%s %1.2f%%</option>', $category['cat_id'], (in_array($category['cat_id'], $user['categories']) ? ' selected' : ''),  $category['cat_desc'], $cat_scores_indexed[$category['cat_id']]) . "\r\n";
}
echo '</select>' . "\r\n";
echo '<!-- categories select here -->' . "\r\n";
echo '<input type="submit" value="Save"/>' . "\r\n";
echo '</form>' . "\r\n";
echo '<a href="hangman.html">Play the game</a>' . "\r\n";
echo '</body>' . "\r\n";
echo '</html>' . "\r\n";


?>
